<template>
  <!-- 订单入库 -->
  <view class="storage-entry">
    <view v-for="(group, groupIndex) in productGroups" :key="groupIndex" class="product-group">
      <view class="group-header">
        <view @click="handleGroupSelect(groupIndex)">
          <image class="icon-width mr-8" v-if="group.selected" :src="IMG_URL2 + 'login/checkbox1.svg'" />
          <image class="icon-width mr-8" v-else :src="IMG_URL2 + 'login/checkbox.svg'" />
        </view>
         <u-image :src="group.image" width="96rpx" height="96rpx" radius="8"></u-image>
        <view>{{ group.name }}</view>
      </view>
      <view class="product-items">
        <view v-for="(item, itemIndex) in group.skuList" :key="itemIndex" class="product-item">
          <view class="item-header">
            <view @click="handleItemSelect(groupIndex, itemIndex)">
              <image class="icon-width mr-8" v-if="item.selected" :src="IMG_URL2 + 'login/checkbox1.svg'" />
              <image class="icon-width mr-8" v-else :src="IMG_URL2 + 'login/checkbox.svg'" />
            </view>
            <u-image :src="item.image" width="48rpx" height="48rpx" radius="8"></u-image>
            <view class="spec">{{ item.spec }}</view>
          </view>
          <view class="item-info">
            <view class="specs">
              <view class="orderquantity">订单数量: {{ group.totalQuantity }}</view>
            </view>
            <view class="quantity-control">
              <view class="quantitynums">入库数量</view>
              <u-number-box
                v-model="item.quantity"
                min="1"
                :step="1"
                :input-width="32"
                :buttonSize="20"
              ></u-number-box>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="bottom-button">
      <button class="confirm-btn" @click="handleConfirm">确定</button>
    </view>
    <!-- 入库成功 -->
    <view class="success-page" v-if="showSuccessPage">
      <view class="success-icon">
        <u-icon name="checkmark-circle" color="rgba(43, 164, 113, 1)" size="80"></u-icon>
      </view>
      <text class="success-text">入库成功</text>
      <view class="button-group">
        <button class="action-btn secondary-btn" @click="handleComplete">完成</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { IMG_URL2 } from "@/config";
import { shopApi } from "@/api";
const buttonStyle = {
  width: "90%",
  marginTop: "30rpx",
  marginBottom: "30rpx"
};
let showSuccessPage = ref(false);
const shopId = ref("");

const productGroups = ref([]);
let orderId =ref('')
const handleGroupSelect = (groupIndex) => {
  productGroups.value[groupIndex].selected = !productGroups.value[groupIndex].selected;
  productGroups.value[groupIndex].skuList.forEach((item) => {
    if (productGroups.value[groupIndex].selected) {
      item.selected = true;
    } else {
      item.selected = false;
    }
  });
};

const handleItemSelect = (groupIndex, itemIndex) => {
  const group = productGroups.value[groupIndex];
  group.skuList[itemIndex].selected = !group.skuList[itemIndex].selected;
  group.selected = group.skuList.every((item) => item.selected);
};

const handleConfirm = () => {
  const inStockProducts = productGroups.value.flatMap((group) => 
    group.skuList
      .filter((item) => item.selected && item.quantity > 0) // Ensure inStockQuantity is greater than 0
      .map((item) => ({
        image: item.image || "", // Assuming image is a property of item
        inStockQuantity: item.quantity || 0, // Assuming inStockQuantity is a property of item
        orderId:  orderId.value , // Default value as per instruction
        price: group.price || 0, // Assuming price is a property of item
        productCode: group.productCode || "", // Assuming productCode is a property of item
        productId: group.productId || 0, // Assuming productId is a property of item
        productName: group.name || "", // Assuming productName is a property of item
        productSkuCode: item.productSkuCode || "", // Assuming productSkuCode is a property of item
        productSkuId: item.productSkuId || 0, // Assuming productSkuId is a property of item
        spec: item.spec || "" // Assuming spec is a property of item
      }))
  );

  console.log("In Stock Products:", inStockProducts);

  if (inStockProducts.length === 0) {
    uni.showToast({
      title: "请至少选择一个库存数量大于0的商品",
      icon: "none"
    });
    return;
  }

  console.log("In Stock Products:", inStockProducts);

  shopApi.SHOPSTOCKINSTOCK({
      inStockProducts,
      inStockType: 2,
    })
    .then((res) => {
      if (res.code != 200) {
        uni.showToast({
          title: res.errMsg,
          icon: "none"
        });
        return;
      }
      uni.showToast({
        title: "入库成功",
        icon: "success"
      });
      showSuccessPage.value = true;
    });
};
const handleComplete = () => {
  // 回到列表页
  uni.navigateBack({
    delta: 1
  });
};
onLoad((options) => {
  // 获取订单详情
  shopId.value = options.id;
  getDetail();
});
const getDetail = () => {
  // 调用接口获取订单详情
  shopApi.ORDERGETORDERDETAIL({ id: shopId.value }).then((res) => {
    if (res.code != 200) {
      uni.showToast({
        title: res.errMsg,
        icon: "none"
      });
      return;
    }
    orderId.value = res.result.orderId;
    productGroups.value = res.result.productList.map((item) => {
      const skuList = item.skuList.map((sku) => {
        return {
          ...sku,
          selected: false
        };
      });
      return {
        ...item,
        selected: false,
        skuList
      };
    });
  });
};
</script>

<style lang="scss" scoped>
.storage-entry {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 20rpx;
  box-sizing: border-box;
  .product-group {
    background-color: #ffffff;
    border-radius: 12rpx;
    margin-bottom: 20rpx;
    padding: 24rpx;
    box-sizing: border-box;
    .group-header {
      display: flex;
      align-items: center;
      gap: 20rpx;
      padding-bottom: 24rpx;

      .icon-width {
        width: 40rpx;
        height: 40rpx;
      }
    }

    .product-items {
      .product-item {
        padding: 24rpx 0;
        border-bottom: 2rpx solid #f5f5f5;

        &:last-child {
          border-bottom: none;
        }

        .item-header {
          display: flex;
          align-items: center;
          gap: 20rpx;
          margin-bottom: 20rpx;

          .icon-width {
            width: 40rpx;
            height: 40rpx;
          }
          .spec {
            font-family: PingFang SC;
            font-size: 12px;
            font-weight: 400;
            line-height: 20px;
            text-align: left;
            text-underline-position: from-font;
            text-decoration-skip-ink: none;
            color: rgba(0, 0, 0, 0.9);
          }
        }

        .item-info {
          padding-left: 54rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          box-sizing: border-box;
          .specs {
            display: flex;
            gap: 20rpx;
            margin-bottom: 20rpx;
            .orderquantity {
              font-family: PingFang SC;
              font-size: 12px;
              font-weight: 400;
              line-height: 20px;
              text-align: left;
              text-underline-position: from-font;
              text-decoration-skip-ink: none;
              color: rgba(0, 0, 0, 0.6);
            }
          }

          .quantity-control {
            display: flex;
            align-items: center;
            .quantitynums {
              font-family: PingFang SC;
              font-size: 12px;
              font-weight: 400;
              line-height: 20px;
              text-align: left;
              text-underline-position: from-font;
              text-decoration-skip-ink: none;
              color: rgba(0, 0, 0, 0.6);
              margin-right: 10rpx;
            }
          }
        }
      }
    }
  }

  .bottom-button {
    position: fixed;
    bottom: 0rpx;
    left: 0;
    right: 0;
    padding: 24rpx 24rpx 44rpx 24rpx;
    box-sizing: border-box;
    background-color: #fff;
    box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    z-index: 9;

    .confirm-btn {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background-color: rgba(214, 142, 68, 1);
      color: #fff;
      border: none;
      border-radius: 40rpx;
      font-size: 32rpx;

      &:active {
        opacity: 0.9;
      }
    }
  }

  .success-page {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 120rpx 40rpx;
    box-sizing: border-box;
    z-index: 99;
    width: 100%;
    height: 100vh;
    .success-icon {
      margin-bottom: 40rpx;
    }

    .success-text {
      font-size: 36rpx;
      color: #333;
      font-weight: bold;
      margin-bottom: 100rpx;
    }

    .button-group {
      width: 100%;
      display: flex;
      flex-direction: column;
      gap: 20rpx;
      .action-btn {
        width: 100%;
        height: 88rpx;
        line-height: 88rpx;
        border-radius: 44rpx;
        font-size: 32rpx;
        border: none;

        &.primary-btn {
          background-color: rgba(214, 142, 68, 1);
          color: #fff;
        }

        &.secondary-btn {
          background-color: rgba(253, 236, 219, 1);
          color: rgba(214, 142, 68, 1);
        }
      }
    }
  }
}
</style>
